<?php
namespace app\controller\api\v1\housing;

use app\Request;
use app\validate\api\housing\HousingPersonalAuthenticationValidate;
use think\facade\Db;

class HousingPersonalAuthentication
 {


	 public function Authentication_Add (Request $request)
	 {
		 $request_data = $request->postMore([['Identity_type',''],['name',''],['phone',''],['Identity_type',''],['Identity_id',''],['identity_positive',''],['identity_reverse',''],],true);

		 validate(HousingPersonalAuthenticationValidate::class)->check($request_data);

		 $data =$request_data;
		 $data['user_id']=$request->uid;
		 try {
			 Db::startTrans();
			 $Model = new  \app\model\v1\housing\HousingPersonalAuthentication();
			 $person =  $Model->where ('user_id',$request->uid)->find ();
			 if (!$person){
				 $Model->insert ($data);
			 }elseif($person['status']===2||$person['status']===1){
				 $ser = $Model->where ('id',$person['id'])->update($data);
				 if ($ser==0){
					 Db::rollback();
					 return app('json')->status('success','请重新提交个人信息');
				 }
				 $status['status']=0;
				 $Model->where ('id',$person['id'])->update($status);
			 }elseif($person['status']===0){
				 return app('json')->status('success','您已申请个人认证，请等待');

			 }
			 Db::commit();
		 } catch (\Exception $e) {
			 Db::rollback();
			 $result['msg'] = $e->getMessage();
			 return app('json')->fail($result['msg']);
		 }

		 return app('json')->status('success');

	 }

	 public function get_user_authentication (Request $request)
	{
		$Model = new  \app\model\v1\housing\HousingPersonalAuthentication();
		$person =  $Model->where ('user_id',$request->uid)->find ();
		if (!$person){
			Ajaxreturn::return_succes (true,'暂无数据','');
		}elseif($person['status']===2){
			Ajaxreturn::return_succes (false,'您申请个人认证已拒绝',$person);
		}elseif($person['status']===0){
			Ajaxreturn::return_succes (true,'您已申请个人认证，请等待',$person);
		}elseif($person['status']===1){
			Ajaxreturn::return_succes (true,'您已认证成功，‘请勿重复提交',$person);
		}
	}
}
